home *** CD-ROM | disk | FTP | other *** search
- Path: newshost.lanl.gov!tanmoy
- From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
- Newsgroups: comp.lang.pl1,comp.lang.c
- Subject: Re: PL/I and C
- Date: 27 Feb 1996 23:32:45 GMT
- Organization: Los Alamos National Laboratory
- Distribution: world
- Message-ID: <TANMOY.96Feb27163245@qcd.lanl.gov>
- References: <4gh5ru$eng@goanna.cs.rmit.EDU.AU> <312CCEB2.4AB7@corp.dialog.com>
- <AD536AAB9668B76CD@mcdialb09.it.luc.edu>
- <312E363C.3CDE@corp.dialog.com>
- <AD53AB1396681879FA@mcdialb10.it.luc.edu>
- <4gsgrd$v7u@zk2nws.zko.dec.com>
- NNTP-Posting-Host: qcd.lanl.gov
- Mime-Version: 1.0
- Content-Type: text
- In-reply-to: zeeb@jacare.zk3.dec.com's message of 26 Feb 1996 14:41:49 GMT
-
- In article <4gsgrd$v7u@zk2nws.zko.dec.com>
- zeeb@jacare.zk3.dec.com (Jeff Zeeb DEC C) writes:
-
- JZDC: >
- JZDC: > foo *x;
- JZDC: > bar *y;
- JZDC: >
- JZDC: > y=(bar *) x;
- JZDC:
- JZDC: This is dangerous territory. The code fragment above violates the
- JZDC: aliasing rules in the ANSI C standard. In a nutshell, those rules
- JZDC: state that a pointer to one type will not point to an object of a
- JZDC: different type. An optimizer could generate code using this assumption,
- JZDC: which would have unexpected results in your program.
-
- Actually that is not quite correct. The relevant guarantee in C is
- that an object defined to be of a certain type shall be accessed with
- an lvalue of a compatible type, or by an lvalue of a character
- type. (Actually, the rules are a bit more precise: qualified and
- unqualified versions of a type are allowed, as are access as a part of
- a union, array or struct whose elements/fields contains the actual
- type).
-
- For example, if all that the compiler saw was the following:
-
- long xx = 0;
- short yy = 0;
- long *x = &xx;
- short *y;
- y = (short*) x;
- f(y);
-
- It cannot assume that xx is unchanged after the call. The reason is
- that f may do a *(long*)y and hence change y.
-
- On the other hand, if the `f(y);' were replaced by
-
- *y = yy;
-
- The compiler could have assumed xx hasn't changed! The reason is that
- *y is of type short, and is not allowed to be used to change xx which
- is of type long.
-
- On the other hand, if short was changed to char consistently above,
- again, the compiler had to play safe. A character lvalue can be used
- to change a long.
-
- Cheers
- Tanmoy
- --
- tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
- Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
- Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
- <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
- internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
- fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
-